#include<U8g2lib.h>
#include<Arduino.h>
#include "DHT.h"



#define DHTPIN 12//温度引脚
#define PINLED 8
byte jdPin = 9; //继电器模块
byte pwmPin = 10;
byte tLight=HIGH;
float agCal;//模拟光敏
float qagCal;//气体传感器模拟
float h;//湿度
float t;//温度
float hcaCal;//人体传感器
String readClientData = "";
#define DHTTYPE DHT11//传感器类型

static const uint8_t myText[][32] = {

  { 0x00, 0x00, 0xC4, 0x1F, 0x48, 0x10, 0x48, 0x10, 0xC1, 0x1F, 0x42, 0x10, 0x42, 0x10, 0xC8, 0x1F,
    0x08, 0x00, 0xE4, 0x3F, 0x27, 0x25, 0x24, 0x25, 0x24, 0x25, 0x24, 0x25, 0xF4, 0x7F, 0x00, 0x00
  },/*"温",0*/

  { 0x00, 0x00, 0xE4, 0x1F, 0x28, 0x10, 0x28, 0x10, 0xE1, 0x1F, 0x22, 0x10, 0x22, 0x10, 0xE8, 0x1F,
    0x88, 0x04, 0x84, 0x04, 0x97, 0x24, 0xA4, 0x14, 0xC4, 0x0C, 0x84, 0x04, 0xF4, 0x7F, 0x00, 0x00
  },/*"湿",1*/

  { 0x80, 0x00, 0x00, 0x01, 0xFC, 0x7F, 0x44, 0x04, 0x44, 0x04, 0xFC, 0x3F, 0x44, 0x04, 0x44, 0x04,
    0xC4, 0x07, 0x04, 0x00, 0xF4, 0x0F, 0x24, 0x08, 0x42, 0x04, 0x82, 0x03, 0x61, 0x0C, 0x1C, 0x70
  },/*"度",2*/

  { 0x40, 0x00, 0x80, 0x00, 0xFE, 0x7F, 0x02, 0x40, 0x01, 0x20, 0xFC, 0x1F, 0x20, 0x00, 0x10, 0x04,
    0xF8, 0x0F, 0x80, 0x08, 0x80, 0x00, 0xFC, 0x1F, 0x80, 0x00, 0x80, 0x00, 0xFF, 0x7F, 0x00, 0x00
  },/*"室",0*/

  { 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0xFE, 0x3F, 0x82, 0x20, 0x82, 0x20, 0x82, 0x20, 0x42, 0x21,
    0x42, 0x22, 0x22, 0x24, 0x12, 0x28, 0x0A, 0x28, 0x02, 0x20, 0x02, 0x20, 0x02, 0x28, 0x02, 0x10
  },/*"内",1*/

  { 0x40, 0x00, 0x80, 0x00, 0xFE, 0x7F, 0x02, 0x40, 0x11, 0x24, 0x08, 0x08, 0x04, 0x10, 0x00, 0x00,
    0xF8, 0x0F, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0xFE, 0x3F, 0x00, 0x00
  },/*"空",2*/

  { 0x08, 0x00, 0x08, 0x00, 0xFC, 0x3F, 0x04, 0x00, 0xF2, 0x0F, 0x01, 0x00, 0xFC, 0x0F, 0x00, 0x08,
    0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x50, 0x00, 0x50, 0x00, 0x60, 0x00, 0x40
  },/*"气",3*/
  { 0x40, 0x00, 0x40, 0x00, 0xFF, 0x7F, 0x20, 0x00, 0x20, 0x00, 0xF0, 0x0F, 0x10, 0x08, 0x18, 0x08,
    0xF4, 0x0F, 0x12, 0x08, 0x11, 0x08, 0xF0, 0x0F, 0x10, 0x08, 0x10, 0x08, 0x10, 0x0A, 0x10, 0x04
  },/*"有",0*/

  { 0x00, 0x00, 0xFC, 0x0F, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0xFE, 0x3F, 0x20, 0x01,
    0x20, 0x01, 0x20, 0x01, 0x10, 0x01, 0x10, 0x01, 0x08, 0x21, 0x04, 0x21, 0x02, 0x3E, 0x01, 0x00
  },/*"无",1*/

  { 0x80, 0x00, 0xFC, 0x1F, 0x80, 0x00, 0xF8, 0x0F, 0x80, 0x00, 0xFE, 0x3F, 0x00, 0x00, 0xF8, 0x0F,
    0x48, 0x08, 0x88, 0x08, 0xFF, 0x7F, 0x44, 0x08, 0x84, 0x08, 0xFC, 0x3F, 0x00, 0x08, 0x00, 0x06
  },/*"毒",2*/
  { 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x40, 0x01, 0x40, 0x01,
    0x20, 0x02, 0x20, 0x02, 0x10, 0x04, 0x10, 0x04, 0x08, 0x08, 0x04, 0x10, 0x02, 0x20, 0x01, 0x40
  },/*"人",0*/

  { 0x10, 0x02, 0x10, 0x02, 0x10, 0x02, 0x08, 0x02, 0xE8, 0x3F, 0x0C, 0x02, 0x0C, 0x07, 0x0A, 0x07,
    0x89, 0x0A, 0x88, 0x0A, 0x48, 0x12, 0xA8, 0x2F, 0x18, 0x42, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02
  },/*"体",1*/

  { 0x00, 0x14, 0x00, 0x24, 0xFC, 0x7F, 0x04, 0x04, 0xF4, 0x25, 0x04, 0x24, 0xF4, 0x15, 0x14, 0x19,
    0x14, 0x49, 0xF2, 0x55, 0x02, 0x62, 0x01, 0x41, 0x80, 0x00, 0x12, 0x21, 0x12, 0x48, 0xE1, 0x4F
  },/*"感",2*/

  { 0x80, 0x00, 0x00, 0x01, 0xFC, 0x7F, 0x04, 0x00, 0x04, 0x00, 0x84, 0x20, 0x14, 0x21, 0x24, 0x21,
    0x24, 0x12, 0x44, 0x12, 0x44, 0x08, 0x44, 0x08, 0x02, 0x04, 0x02, 0x02, 0xF9, 0x7F, 0x00, 0x00
  },/*"应",3*/

  { 0x80, 0x00, 0x84, 0x10, 0x88, 0x10, 0x90, 0x08, 0x90, 0x04, 0x80, 0x00, 0xFF, 0x7F, 0x20, 0x02,
    0x20, 0x02, 0x20, 0x02, 0x20, 0x02, 0x10, 0x42, 0x10, 0x42, 0x08, 0x42, 0x04, 0x7C, 0x03, 0x00
  },/*"光",0*/

  { 0x08, 0x02, 0x08, 0x04, 0x08, 0x04, 0xC8, 0x7F, 0x5F, 0x40, 0x28, 0x29, 0x88, 0x10, 0x58, 0x20,
    0x0C, 0x00, 0x8B, 0x3F, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0xEA, 0x7F, 0x04, 0x00
  },/*"控",1*/

  { 0x08, 0x00, 0x08, 0x00, 0xC8, 0x7F, 0x08, 0x04, 0x2A, 0x04, 0x1A, 0x04, 0x0A, 0x04, 0x09, 0x04,
    0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x14, 0x04, 0x24, 0x04, 0x22, 0x04, 0x02, 0x05, 0x01, 0x02
  },/*"灯",2*/

  { 0x00, 0x00, 0xFE, 0x3F, 0x10, 0x04, 0x10, 0x04, 0x10, 0x04, 0x10, 0x04, 0x10, 0x04, 0xFF, 0x7F,
    0x10, 0x04, 0x10, 0x04, 0x10, 0x04, 0x10, 0x04, 0x08, 0x04, 0x08, 0x04, 0x04, 0x04, 0x02, 0x04
  },/*"开",3*/

  { 0x08, 0x08, 0x10, 0x08, 0x10, 0x04, 0x00, 0x00, 0xFC, 0x1F, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
    0xFF, 0x7F, 0x80, 0x00, 0x40, 0x01, 0x40, 0x01, 0x20, 0x02, 0x10, 0x04, 0x0C, 0x18, 0x03, 0x60
  },/*"关",4*/

};

U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI u8g2( U8G2_R0,/* clock=*/ 6, /* data=*/ 5, /* cs=*/ 2, /* dc=*/ 3, /* reset=*/ 4);
DHT dht(DHTPIN, DHTTYPE);//温度读取对象

void setup() {
  // put your setup code here, to run once:
  pinMode(PINLED, OUTPUT);
  pinMode(jdPin, OUTPUT);
  digitalWrite(jdPin, HIGH);

  u8g2.begin();
  dht.begin();
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.enableUTF8Print();
  Serial.begin(115200);

}

void loop() {
  readSensor();//获取传感器值
  String msg = String(t) + "&" + String(h) + "&"  + String(agCal) + "&" + String(qagCal) + "&" + String(hcaCal)+ "&" + String(tLight);
  Serial.println(msg);//串口发送数据


  readClient();//读取客户端发来的消息
  phoLight();//判断关照亮灯
  oledDisplay(); //判断显示温度
  pwmLed();//呼吸灯
  readClientData = "";
}

void readClient() {
  //  读取客户端发来的指令
  if (Serial.available()) {
    while (true) {
      //    读取客户端发来的消息
      char c = Serial.read();

      if (c == '\n' || c == '\r') {
        //        读取完一行
        //        Serial.print(readClientData);
        //        index = readClientData.toInt();
        if (readClientData == "0") {
          //          我的台灯
          //            关灯
          tLight=LOW;
          digitalWrite(jdPin, tLight);
        }
        if (readClientData == "1") {
          //              开灯
          tLight=HIGH;
          digitalWrite(jdPin, tLight);
        }

        break;
      }

      readClientData += c;
    }
  }
}

void readSensor() {
  //获取温度
  float V = 5; //电压
  float B = 1024; //10位 1024
  h = dht.readHumidity(); //湿度
  t = dht.readTemperature(); //温度
  //获取光敏传感器
  int ag = analogRead(A0); //光敏传感器
  agCal = (V / B) * ag;

  //获取气体传感器
  int qag = analogRead(A1);
  qagCal = (V / B) * qag; //得出具体电压值

  //获取人体传感器数值
  int hca = analogRead(A2);
  hcaCal = (V / B) * hca; //得出具体电压值
}

void oledDisplay() {
  if (isnan(h) || isnan(t)) {
    Serial.println("DHT11 faild!");
    u8g2.firstPage();
    do {

      u8g2.drawStr(0, 32, "DHT11 faild");

    } while (u8g2.nextPage());
    return ;
  } else {
    //    显示温湿度
    u8g2.firstPage();
    do {
      //      温度
      u8g2.drawXBM(0, 0, 16, 16, myText[0]);
      u8g2.drawXBM(16, 0, 16, 16, myText[2]);

      u8g2.drawXBM(70, 0, 16, 16, myText[1]);
      u8g2.drawXBM(86, 0, 16, 16, myText[2]);

      u8g2.setCursor(32, 16);
      u8g2.print(t);

      u8g2.setCursor(102, 16);
      u8g2.print(h);

      //气体传感器
      u8g2.drawXBM(0, 16, 16, 16, myText[3]);
      u8g2.drawXBM(16, 16, 16, 16, myText[4]);
      u8g2.drawXBM(32, 16, 16, 16, myText[5]);
      u8g2.drawXBM(48, 16, 16, 16, myText[6]);
      if (qagCal < 1.5) {
        //        无毒
        u8g2.drawXBM(96, 16, 16, 16, myText[8]);
        u8g2.drawXBM(112, 16, 16, 16, myText[9]);
      } else {
        //          有毒
        u8g2.drawXBM(96, 16, 16, 16, myText[7]);
        u8g2.drawXBM(112, 16, 16, 16, myText[9]);
      }

      //      人体感应
      u8g2.drawXBM(0, 32, 16, 16, myText[10]);
      u8g2.drawXBM(16, 32, 16, 16, myText[11]);
      u8g2.drawXBM(32, 32, 16, 16, myText[12]);
      u8g2.drawXBM(48, 32, 16, 16, myText[13]);
      if (hcaCal < 3.0) {
        //        无人
        u8g2.drawXBM(96, 32, 16, 16, myText[8]);
        u8g2.drawXBM(112, 32, 16, 16, myText[10]);
      } else {
        //          有人
        u8g2.drawXBM(96, 32, 16, 16, myText[7]);
        u8g2.drawXBM(112, 32, 16, 16, myText[10]);
      }
      //光控灯
      u8g2.drawXBM(0, 48, 16, 16, myText[14]);
      u8g2.drawXBM(16, 48, 16, 16, myText[15]);
      u8g2.drawXBM(32, 482, 16, 16, myText[16]);
      if (agCal < 3.0) {
        u8g2.drawXBM(112, 48, 16, 16, myText[18]);
      } else {
        u8g2.drawXBM(112, 48, 16, 16, myText[17]);
      }


    } while (u8g2.nextPage());
  }


}

void phoLight() {
  //    光敏传感器
  if (agCal < 3.0) {
    digitalWrite(PINLED, LOW);
  } else {
    digitalWrite(PINLED, HIGH);
  }
}

void pwmLed() {
  for (int fadeValue = 0; fadeValue < 255; fadeValue += 10) {
    analogWrite(pwmPin, fadeValue);
    delay(10);
  }


  for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 10) {
    analogWrite(pwmPin, fadeValue);
    delay(10);
  }

}
